import puppeteer from 'puppeteer-core';
import { getChromePath, getChromeExtensions, getDefaultChromeUserDataDir, getExtensionArgs } from '../main/utils/browser';
import path from 'path';
import fs from 'fs';

async function testLoadExtensions() {
  const extensions = getChromeExtensions();
  console.log('检测到的扩展列表:', extensions);

  const extensionArgs = getExtensionArgs();
  console.log('扩展参数:', extensionArgs);

  const browser = await puppeteer.launch({
    executablePath: getChromePath(),
    headless: false,
    userDataDir: getDefaultChromeUserDataDir(), // 使用独立用户目录避免冲突
    args: [
      // '--load-extension=/Users/chen/Library/Application Support/Google/Chrome/Default/Extensions/ihnfmldlpdipgnliolhfffenpcmjgnif/3.2.4_0',
      // '--disable-extensions-except=' + extensionArgs.map(arg => arg.split('=')[1]).join(','),
      '--no-first-run' // 禁用首次运行提示
    ],
    ignoreDefaultArgs: ['--disable-extensions'] // 必须禁用默认的扩展禁用设置
  });

  console.log('浏览器启动参数:', browser.process()?.spawnargs);

  const page = await browser.newPage();
  await page.goto('chrome://extensions');
  
  // 添加调试信息验证扩展加载
  // const extensionsInfo = await page.evaluate(() => {
  //   return Array.from(document.querySelectorAll('extensions-item')).map(el => el.shadowRoot?.textContent);
  // });
  // console.log('已加载扩展:', extensionsInfo.filter(Boolean));

  // await page.waitForTimeout(5000); // 留出时间观察扩展加载
  // await browser.close();
}

testLoadExtensions().catch(console.error);